// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
         roleAnim: {
            type: cc.Animation,
            default: null
         }
    },

    ctor() {
        this.startJumpVelocity = 850 //起跳时的初速度 像素每秒 决定了起跳高度
        this.pullForce = 2000 //纵向加速度 像素每秒

        this.speed = new cc.Vec2(256, 0)
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {
        this.gameScene = ck.getGameScene()
        this.roleAnim = cc.find('role1', this.node).getComponent(cc.Animation)

        this.gameScene.node.on(ck.Event.GameStateChanged, this.onGameStateChanged, this)

        this.setHDirection(ck.PlayerDirection.Right)
        this.stopJump()
        this.setNeedDropDown(true)

        // this.node.on(cc.Node.EventType.POSITION_CHANGED, this.onPositionChanged, this);
    },

    onPositionChanged () {
        ck.log('onPositionChanged', `frame=${cc.director.getTotalFrames()}`, this.node.position)

    },

    onGameStateChanged(){
        if (this.gameScene.data.state === ck.GameState.TouchDoor) {
            ck.log('Player.active = false')
            this.node.active = false
        }
    },

    setDead() {
        if (this.dead) {
            return
        }

        this.dead = true
        this.gameScene.data.setState(ck.GameState.Dead)

        if (this.getHDirection() === ck.PlayerDirection.Left) {
            this.roleAnim.stop()
            this.node.runAction(cc.sequence([
                cc.delayTime(0.1),
                cc.spawn([
                    cc.jumpBy(0.8, 100, -400, 200, 1),
                    cc.rotateBy(0.8, -180),
                ])
            ]))
        }
        else{
            this.roleAnim.stop()
            this.node.runAction(cc.sequence([
                cc.delayTime(0.1),
                cc.spawn([
                    cc.jumpBy(0.8, -100, -400, 200, 1),
                    cc.rotateBy(0.8, 180),
                ])
            ]))
        }

        this.speed.x = this.speed.y = 0

    },

    setHDirection(dir) {
        if (dir === ck.PlayerDirection.Left) {
            this.node.scaleX = -1
            this.speed.x = -Math.abs(this.speed.x)
        }
        else if (dir === ck.PlayerDirection.Right) {
            this.node.scaleX = 1
            this.speed.x = Math.abs(this.speed.x)
        }
    },

    getHDirection() {
        if (this.speed.x < 0) {
            return ck.PlayerDirection.Left
        }
        else if (this.speed.x > 0) {
            return ck.PlayerDirection.Right
        }
        else {
            return null
        }
    },
    getVDirection() {
        if (this.speed.y < 0) {
            return ck.PlayerDirection.Down
        }
        else if (this.speed.y > 0){
            return ck.PlayerDirection.Up
        }
        else {
            return null
        }
    },

    // 是否可以由用户操作跳起
    canJump () {
        if (this.dead)
            return false
        return !this.jumping
    },
    startJump() {
        this.speed.y = this.startJumpVelocity
        this.jumping = true
        this.setNeedDropDown(true)
        cc.audioEngine.play(this.gameScene.jumpSound)
    },
    stopJump() {
        this.speed.y = 0
        this.jumping = false
    },

    putColliderAboveWorldY(worldY) {
        var localY = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(0, worldY)).y
        localY -= this.node.getComponent(cc.Collider).offset.y
        localY += this.node.getComponent(cc.Collider).size.height/2
        // localY += 1
        this.node.y = localY

        // ck.log('putColliderAboveWorldY', `frame=${cc.director.getTotalFrames()}`, this.node.position)
    },

    putColliderAbovePlatformWorldY(worldY) {
        this.putColliderAboveWorldY(worldY)
        this.stopJump()
        this.setNeedDropDown(false)
    },

    getWorldPosition() {
        return this.node.convertToWorldSpaceAR(new cc.Vec2(0,0))
    },

    setNeedDropDown(need) {
        this.needDropDown = need

        ck.log('player.setNeedDropDown', this.needDropDown)

        if (need) {
        }
    },

    putColliderUnderWorldY(worldY) {
        var localY = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(0, worldY)).y
        localY += this.node.getComponent(cc.Collider).offset.y
        localY -= this.node.getComponent(cc.Collider).size.height/2
        localY -= 1
        this.node.y = localY

        // ck.log('putColliderUnderWorldY', `frame=${cc.director.getTotalFrames()}`, this.node.position)

    },

    update (dt) {

        // ck.log('update1', `frame=${cc.director.getTotalFrames()}`, this.node.position)

        if (this.gameScene.data.state === ck.GameState.Prepare)
            return

        if (this.gameScene.data.state === ck.GameState.Finished)
            return

        if (this.gameScene.data.state === ck.GameState.Dead)
            return

        if (!this.needDropDown) {
            this.speed.y = 0
        }
        else{
            this.speed.y -= dt*this.pullForce
        }

        var diff = this.speed.clone().multiplyScalar(dt)

        // ck.log('player.update', `speed.y=${this.speed.y}`, `diff.y=${diff.y}`, `pos.y=${this.node.y}`)

        this.node.position = this.node.position.add(diff)

        var border_leftbottom = this.gameScene.border.getLeftBottomRelativeNodeParent(this.node)
        var border_rightbottom = this.gameScene.border.getRightBottomRelativeNodeParent(this.node)
        if (this.node.position.x < border_leftbottom.x) {
            this.node.x = border_rightbottom.x
            this.node.y += 1
            // ck.log('player.update touch border1')
        }
        if (this.node.position.x > border_rightbottom.x) {
            this.node.x = border_leftbottom.x
            this.node.y += 1
            // ck.log('player.update touch border2')

        }

        // ck.log('update2', `frame=${cc.director.getTotalFrames()}`, this.node.position)
    },

    // lateUpdate() {
    //     ck.log('lateUpdate', `frame=${cc.director.getTotalFrames()}`, this.node.position)
    //     ck.log('------------------------------')
    // },

});
